package com.first.agent.dao.impl;

import com.first.agent.dao.UserManagementDao;
import com.first.agent.dbutils.JdbcBuild;
import com.first.agent.pojo.AgentDetails;
import com.first.agent.pojo.AgentDetailsJ;
import com.first.agent.pojo.User;
import com.first.agent.pojo.UserJ;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;


public class UserManagementDaoImpl extends JdbcBuild implements UserManagementDao {
    @Override
    public List<UserJ> findAll(int page, int limit) throws SQLException {
        String sql = "SELECT id,username,password,r.role_name role, nickname,creation_time,balance,state FROM user u LEFT JOIN role r on u.u_ro_id=r.role_id limit ?,?";
        Object[] params = {(page-1)*limit, limit};
        List<UserJ> userList = super.queryListEntity(UserJ.class, sql, params);
        return userList;
    }


    @Override
    public int findCount() throws SQLException {
        String sql = "select count(*) FROM user";
        int i = super.findAllForInt(sql, null);
        return i;
    }

    @Override
    public UserJ findUser(int id) throws SQLException {
        String sql = "SELECT id,username,password,r.role_name role, nickname,creation_time,balance,state FROM user u LEFT JOIN role r on u.u_ro_id=r.role_id where id = ?";
        Object[] params = {id};
        UserJ userJ = super.queryEntity(UserJ.class, sql, params);
        System.out.println(userJ);
        return userJ;
    }

    //删除一条记录
    @Override
    public int delUser(int id) throws SQLException {
        String sql = "DELETE FROM USER WHERE id = ?";
        Object[] params = {id};
        int i = super.executeUpdate(sql, params);
        System.out.println(i);
        return i;
    }

    //通过role查询role对应的role_id
    @Override
    public int findRoleIdByRole(String role) throws SQLException {
        String sql = "SELECT role_id FROM role where role_name='?'";
        Object[] params = {role};
        Integer i = super.queryEntity(Integer.class, sql, params);
        return i;
    }

    @Override
    public int modifyById(User user) throws SQLException {
        String sql = "UPDATE user set username =? ,nickname =?, `password` =?, u_ro_id=?, state=? WHERE id=?";
        Object[] params = {user.getUsername(),user.getNickname(),user.getPassword(),user.getU_ro_id(),user.getState(),user.getId()};
        int i = super.executeUpdate(sql, params);
        return 0;
    }

    @Override
    public List<AgentDetails> findAgentDetail(String username) throws SQLException {
        String sql = "SELECT * FROM agent_details WHERE username = ?";
        Object[] params = {username};
        List<AgentDetails> list = super.queryListEntity(AgentDetails.class, sql, params);
        return list;
    }

    @Override
    public List<Map<String, Object>> findAllRole() throws SQLException {
        String sql="SELECT role_id,role_name FROM `role`";
        List<Map<String, Object>> list = super.findAllForList(sql, null);
        System.out.println(list);
        System.out.println("+++++++++++++++++++++++++");
        return list;
    }

    @Override
    public int addUser(User user) throws SQLException {
        String sql = "INSERT into user (username,nickname,`password`,u_ro_id,creation_time,state) VALUES (?,?,?,?,now(),?)";
        Object[] params = {user.getUsername(),user.getNickname(),user.getPassword(),user.getU_ro_id(),user.getState()};
        int i = super.executeUpdate(sql, params);
        return i;
    }

    @Override
    public List<AgentDetailsJ> findAllYu(String username, int page, int limit) throws SQLException {
        System.out.println(username);
        System.out.println(page);
        System.out.println(limit);
        String sql = "SELECT * FROM `agent_details`LEFT JOIN finance_type ON fil_type_id=fi_id where username=? limit ?,?";
        Object[] params = {username,(page-1)*limit, limit};
        List<AgentDetailsJ> list = super.queryListEntity(AgentDetailsJ.class, sql, params);
        System.out.println(list);
        return list;
    }
}
